package com.zjucst.eroares.fileoperator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;

/**
 * @author 作者 E-mail:
 * @date 创建时间：2016年5月29日 下午9:34:14
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class ReadFromFile {
	/**
	 * 
	 * @Title: readFileByRandomAccess
	 * @Description: TODO RandomAccess File
	 * @param fileName
	 * @return: void
	 */
	public static void readFileByRandomAccess(String fileName) {
		RandomAccessFile randomFile = null;
		try {
			randomFile = new RandomAccessFile(fileName, "r");
			Long fileLength = randomFile.length();
			int beginIndex = (fileLength > 4) ? 0 : 0;
			randomFile.seek(beginIndex);
			byte[] bytes = new byte[10];
			int byteread = 0;
			while ((byteread = randomFile.read(bytes)) != -1) {
				System.out.write(bytes, 0, byteread);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			if (randomFile != null) {
				try {
					randomFile.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 
	 * @Title: readFileByLine
	 * @Description: TODO Read File By Line
	 * @param fileName
	 * @return: void
	 */
	public static void readFileByLine(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tmpString = null;
			int line = 1;
			while ((tmpString = reader.readLine()) != null) {
				System.out.println("Line" + line + ":" + tmpString);
				line++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 
	 * @Title: readFileByChars
	 * @Description: TODO read file by char or chars
	 * @param fileName
	 * @return: void
	 */
	public static void readFileByChars(String fileName) {
		File file = new File(fileName);
		Reader reader = null;
		// read a char one time
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			int temchar;
			while ((temchar = reader.read()) != -1) {
				if ((char) temchar != '\r') {
					System.out.println((char) temchar);
				}
			}
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// read some chars one time
		char[] tmpchars = new char[30];
		int charread = 0;
		try {
			reader = new InputStreamReader(new FileInputStream(fileName));
			while ((charread = reader.read(tmpchars)) != -1) {
				if (charread == tmpchars.length && tmpchars[tmpchars.length - 1] != '\r') {
					System.out.println(tmpchars);
				} else {
					for (int i = 0; i < charread; i++) {
						if (tmpchars[i] == '\r') {
							continue;
						} else {
							System.out.print(tmpchars[i]);
						}
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public static void readFileByBytes(String fileName) {
		File file = new File(fileName);
		InputStream in = null;
		// read file by one Byte by one Byte
		try {
			in = new FileInputStream(file);
			int tmpbyte;
			while ((tmpbyte = in.read()) != -1) {
				System.out.write(tmpbyte);
			}
			in.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return;
		}

		// // read file by Bytes
		//
		// try {
		// Byte[] tmpbytes = new Byte[100];
		// int byteread = 0;
		// in = new FileInputStream(fileName);
		// ReadFromFile.showAvailableBytes(in);
		// while ((byteread = in.read(tmpbytes)) != -1) {
		// System.out.write(tmpbytes, 0, byteread);
		// }
		// } catch (FileNotFoundException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

}
